package com.rpoonolly.simplegame;

import java.util.List;

import javax.vecmath.Vector2d;

import com.rpoonolly.simplegame.core.CollisionDetector;
import com.rpoonolly.simplegame.core.CollisionResponder;
import com.rpoonolly.simplegame.core.GameLogicProcessor;
import com.rpoonolly.simplegame.core.aabb.AABBCollision;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.core.aabb.DynamicAABBEntity;

public abstract class SimpleCharacterProcessor implements GameLogicProcessor<AABBEntity> {
	protected CollisionDetector<AABBEntity, AABBCollision> collisionDetector;
	protected CollisionResponder<AABBCollision> collisionResponder;
	
	public SimpleCharacterProcessor(
		CollisionDetector<AABBEntity, AABBCollision> detector, 
		CollisionResponder<AABBCollision> responder
	) {
		collisionDetector = detector;
		collisionResponder = responder;
	}
	
	protected AABBCollision checkWorldCollision(DynamicAABBEntity entity, List<AABBEntity> worldObjs) {
		Vector2d v = entity.getDirection();
		AABBCollision collision, minCollision = null;
		
		for (AABBEntity worldObj : worldObjs) {
			collision = collisionDetector.collisionDetect(worldObj, entity, v);
			
			if (collision == null) continue;
			else if (minCollision == null) minCollision = collision;
			else if (collision.compareTo(minCollision) < 0) {
				minCollision = collision;
				v = minCollision.getMovementVector();
			}
		}
		
		return minCollision;
	}
}
